// hyb_yl/set_equipment/set_equipment.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    page:0,
    last:"",
    userinfo:[],
    j_id:"",
  },
  // 扫码
  saoma:function(e){
    var that = this;
    wx.scanCode({
      success:function(re){
        console.log(re)
        var sn = re.result;
        app.util.request({
          'url':"entry/wxapp/index.binding",
          data:{
            openid:wx.getStorageSync('openid'),
            sn:sn,
            type:e.currentTarget.dataset.type,
            j_id:that.data.j_id
          },
          success:function(res){
            var code = res.data.code;
            if(code == 1)
            {
              wx.showModal({
                title: '提示',
                content:res.data.message,
                showCancel:false,
                success:function(con){
                  if(con.confirm)
                  {
                    that.setData({
                      page:0,
                      list:[]
                    })
                    that.getList();
                  }
                  
                }
              })
            }else if(code == 2)
            {
              app.util.request({
                'url':"entry/wxapp/index.changebundling",
                data:{
                  sn:sn,
                  openid:wx.getStorageSync('openid'),
                  j_id:j_id,
                  type:e.currentTarget.dataset.type,
                  j_id:that.data.j_id
                },
                success:function(res){
                  that.setData({
                    page:0,
                    list:[]
                  })
                  that.getList();
                }
              })
            }else{
              wx.showModal({
                title: '提示',
                content:res.data.message,
                success:function(con){
                  if(con.confirm)
                  {
                    that.setData({
                      page:0,
                      list:[]
                    })
                    that.getList();
                  }
                  
                }
              })
            }
            
          }
        })
      }
    })
  },
  // 选择患者
  choosepeo:function(e){
    this.setData({
      j_id:e.currentTarget.dataset.j_id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  // 获取列表
  getList:function(){
    var that = this;
    app.util.request({
      'url':"enrty/wxapp/index.user_equipment",
      data:{
        openid:wx.getStorageSync('openid'),
        page:that.data.page
      },
      success:function(res){
        var page = that.data.page;
        page = page + 1;
        that.setData({
          list:that.data.list.concat(res.data),
          page:page
        })
      }
    })
  },
  // 解绑
  unbundling:function(e){
    var that = this;
    app.util.request({
      'url':"entry/wxapp/index.unbundling",
      data:{
        id:e.currentTarget.dataset.id,
        openid:wx.getStorageSync('openid'),
      },
      success:function(res){
        wx.showModal({
          title: '提示',
          content:res.data.message,
          showCancel:false,
          success:function(con){
            that.setData({
              page:0,
              list:[]
            })
            that.getList();
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.getList();
    app.util.request({
      'url':"entry/wxapp/user.alluserfamily",
      data:{
        openid:wx.getStorageSync('openid'),
      },
      success:function(res){
        that.setData({
          userinfo:res.data,
        })
        if(res.data.length > 0)
        {
          that.setData({
            j_id:res.data[0].j_id
          })
        }
      }
    })
  },
  // 添加患者信息
  addPeo:function(){
    var sickrela = JSON.stringify(this.data.sickrela)
    var sick_index = e.currentTarget.dataset.sick_index
    console.log(sick_index)
    wx.navigateTo({
      url: "/hyb_yl/userCommunicate/pages/preindex/sickinfo/index?sickrela=" + sickrela + '&sick_index=' + sick_index + '&last=' + this.data.last
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})